package sys

import (
	"WebFacade/log"
	"fmt"
	"github.com/common-nighthawk/go-figure"
	"os"
	"os/signal"
	"syscall"
)

// 阻塞服务 等待 捕捉一个系统信号量退出
func WaitSysSignal(serviceName string, graces ...func()) {
	log.InfoLog(asciiLogo(serviceName), nil)
	log.InfoLog("server is starting", nil)
	caseSign := []os.Signal{syscall.SIGINT, syscall.SIGTERM}
	signChan := make(chan os.Signal)
	signal.Notify(signChan, caseSign...)
	log.InfoLog("catch a system signal, server is quiting", map[string]interface{}{
		"signal": (<-signChan).String(),
	})

	// 下面开始进行优雅关闭操作
	for i := range graces {
		graces[i]()
	}

	log.InfoLog("server grace stop", nil)
}

func asciiLogo(serviceName string) string {
	return fmt.Sprintf("\r\n%v\n", figure.NewFigure(serviceName, "isometric1", true).String())
}
